using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace Programonica.Igramonica.Core
{
    public class Fps : Microsoft.Xna.Framework.DrawableGameComponent
    {
        int framesCount = 0;

        int passedTime = 0;

        int fps = 0;

        private ContentManager content;

        private SpriteBatch spriteBatch;
        
        private SpriteFont font;

        public Fps(Game game)
            : base(game)
        {
            content = Game.Content;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            this.font = this.content.Load<SpriteFont>("Font");

            this.spriteBatch = new SpriteBatch(GraphicsDevice);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            this.passedTime += gameTime.ElapsedRealTime.Milliseconds;

            this.framesCount++;

            if (this.passedTime > 1000)
            {
                this.fps = (int)(this.framesCount * 1000.0f / this.passedTime);

                this.framesCount = 0;

                this.passedTime = 0;
            }

            this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

            this.spriteBatch.DrawString(this.font, this.fps.ToString(), new Vector2(3, 3), Color.White);
            
            this.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}